package com.naiterui.ehp.bs.weixin.handler;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.weixin.service.WechatService;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

/**
 * @author gaoll gll910802@163.com
 */
@Slf4j
@Component
@AllArgsConstructor
public class SubscribeHandler extends AbstractHandler {

    private final WechatService wechatService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
        WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {

        logger.info("SubscribeHandler , wxMessage={}", wxMessage);
        String openId = wxMessage.getFromUser();
        String eventKey = wxMessage.getEventKey();

        try {
            wechatService.subscribe(openId, eventKey, weixinService);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
